package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm01Currency;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm02Currvalue;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class CurrencyValuesFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teesm02Currvalue currencyValue = (Teesm02Currvalue) parameters[0];
        Timestamp fecha = (Timestamp) parameters[1];
        
        Teesm01Currency codeOrig = currencyValue.getEesm02OrigCurrencyCodeC();
        Teesm01Currency codeDest = currencyValue.getEesm02DestCurrencyCodeC();
        Teesg01Plant plant = currencyValue.getEesm02PlantCodeC();
        
        exp = builder.get("eesm02PlantCodeC").equal(plant);
        if(fecha != null){
            exp = exp.and(builder.get("eesm02CurrencyValueDateY").lessThanEqual(fecha));
            exp = exp.and(builder.get("eesm02CurrencyValOutDateY").greaterThan(fecha));
        } 
        if (codeOrig != null) {
            exp = exp.and(builder.get("eesm02OrigCurrencyCodeC").equal(codeOrig));
        }
        if (codeDest != null) {
            exp = exp.and(builder.get("eesm02DestCurrencyCodeC").equal(codeDest));
        }
        return exp;
    }

}
